<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>坦克大战</title>
</head>
<style type="text/css">
    #box{margin-left: 50px}
    #img{float: right;position: absolute;top: 50px;right: 50px;}
    #p1{float: left;
        aposition: absolute;
        left:800px;
        top:20px;}
    #toggle{}
    #tankMap{
        background: url("images/beijing.gif");
    }

</style>
<body onkeydown="getCommand()" onplay="toggleSound()">
<audio src="music/bgm1.mp3"  id="music1" preload="preload" loop="loop" ></audio>
<div align="laft" id="box">
<div id="p1"></div>
    <img src="images/musicpse.png" id="toggle"width="50px" height="50px" onclick="toggleSound()">

<canvas id="tankMap" height="500px" width="800px" >
你的浏览器不支持！！

</canvas>
<div id="img" ><img src="images/操作说明.png" width="299" height="261"></div>
</div>
<script type="text/javascript" src="js/tank.js"></script>
<script type="text/javascript">
    var ctx = document.getElementById("tankMap").getContext("2d");
    var heroX=390;
    var heroY=470;
function toggleSound(){
    var music = document.getElementById("music1");
    var toggle=document.getElementById("toggle");
    if(music.paused){
        music.play();
        toggle.src="images/musicplay.png";
    }else{
        music.pause();
        toggle.src="images/musicpse.png";
    }
}

  var home = new Home();
    //我方坦克
    var hero =new Array();
    //我方子弹
    var heroBullets=new Array();
    //敌人子弹的数组
    var enemyBullets=new Array();
    //敌人坦克
    var enemyTanks = new Array();
    //爆炸
    var bombs=new Array();
    var gameover =new gameoverm();
    var count=6;
    var num = Math.round(Math.random()*100);
function gameinfo(){

    hero.push(new Hero(heroX,heroY,0,"hero",heroColors));
    for(var i=0;i<count;i++){
        var enemyTank=new EnemyTank((i+2)*50,1,2,"enemy",EnemyColors);
        enemyTanks[i]=enemyTank;
        if(enemyTanks[i]!=null){
            window.setInterval("enemyTanks["+i+"].run()",50);
            var eb=new Bullet(enemyTanks[i].X+9,enemyTanks[i].Y+30,2,8,"enemy",enemyTanks[i]);
            enemyBullets[i]=eb;
            var ettimer=window.setInterval("enemyBullets["+i+"].run()",50);
            enemyBullets[i].timer=ettimer;
        }

    }
    count++;
}

    flashTankMap();
    function flashTankMap() {
        if(gameover.isLive){
            ctx.clearRect(0, 0, 800, 500);
            ctx.font="30px 黑体";
            ctx.fillStyle="red";

            ctx.fillText("分数："+(count*100+num)+"分",320,250);
            ctx.fillText("按F5重新开始",320,280);
            ctx.fillText("按J加难度开始",315,310);
        }else {
            if (home.isLine) {
                home.homeimg.onload = function () {
                    ctx.drawImage(home.homeimg, 0, 0, 800, 500);
                }

            } else {
                ctx.clearRect(0, 0, 800, 500);
                drawTank(hero[0]);
                drawHeroBullet();
                isHitEnemyTank();
                drawEnemyBomb();
                drawEnemyBullet();
                isHitHeroTank();

                for (var i = 0; i < enemyTanks.length; i++) {
                    drawTank(enemyTanks[i]);
                }
            }
        }
}

function getCommand() {
    var code = event.keyCode;
    if(hero[0]!=null) {
        switch (code) {
            case 87:
                hero[0].moveUp();
                break;
            case 68:
                hero[0].moveRight();
                break;
            case 83:
                hero[0].moveDown();
                break;
            case 65:
                hero[0].moveLaft();
                break;
            case 74:
                hero[0].shotEnemy();
                break;
        }
    }else if(code==74){

        if(gameover.isLive){
            gameover.isLive=false;
            count=6;
        }
        gameinfo();
        if (home.isLine) {
            home.setisLine();
        }
    }
    flashTankMap();
}

function ifnewgame(){
        if(hero[0]!=null) {
            if (enemyTanks.length == 0) {
                gameinfo();
            }
        }
}

 window.setInterval("flashTankMap(),ifnewgame()",100);


</script>

</body>
</html>